import React, { useEffect, useState } from 'react';
import { Table } from 'antd';
import { eventBus } from '@/utils/event';

const MoneyTable = data => {
  const [dataSource, setSource] = useState([]);

  useEffect(() => {
    eventBus.$on('budge-detail', data => {
      setTable(data);
    });
  }, []);

  const setTable = data => {
    const { lastBudget, addTo } = data;
    if (!lastBudget) {
      return setSource([]);
    }
    const source = [
      {
        from: '年初预算',
        publicFunds: lastBudget.publicFunds,
        currentLevelBudget: lastBudget.currentLevelBudget,
        superiorLevelBudget: lastBudget.superiorLevelBudget,
        selfPrepare: lastBudget.selfPrepare,
        other: lastBudget.other,
        otherRemark: lastBudget.otherRemark,
        total: lastBudget.total,
      },
      {
        from: '中途追加',
        publicFunds: addTo.publicFunds,
        currentLevelBudget: addTo.currentLevelBudget,
        superiorLevelBudget: addTo.superiorLevelBudget,
        selfPrepare: addTo.selfPrepare,
        other: addTo.other,
        otherRemark: addTo.otherRemark,
        total: addTo.total,
      },
      {
        from: '合计',
        publicFunds: lastBudget.publicFunds + addTo.publicFunds,
        currentLevelBudget:
          lastBudget.currentLevelBudget + addTo.currentLevelBudget,
        superiorLevelBudget:
          lastBudget.superiorLevelBudget + addTo.superiorLevelBudget,
        selfPrepare: lastBudget.selfPrepare + addTo.selfPrepare,
        other: lastBudget.other + addTo.other,
        otherRemark: '',
        total: lastBudget.total + addTo.total,
      },
    ];
    setSource(source);
  };

  const columns = [
    {
      title: '资金来源',
      dataIndex: 'from',
      key: 'from',
    },
    {
      title: '公用经费（万元）',
      dataIndex: 'publicFunds',
      key: 'publicFunds',
    },
    {
      title: '本级预算（万元）',
      dataIndex: 'currentLevelBudget',
      key: 'currentLevelBudget',
    },
    {
      title: '上级专项（万元）',
      dataIndex: 'superiorLevelBudget',
      key: 'superiorLevelBudget',
    },
    {
      title: '单位自筹（万元）',
      dataIndex: 'selfPrepare',
      key: 'selfPrepare',
    },
    {
      title: '其他（万元）',
      dataIndex: 'other',
      key: 'other',
    },
    {
      title: '其他来源备注',
      dataIndex: 'otherRemark',
      key: 'otherRemark',
    },
    {
      title: '合计（万元）',
      dataIndex: 'total',
      key: 'total',
    },
  ];

  return <Table dataSource={dataSource} columns={columns} pagination={false} />;
};

export default MoneyTable;
